package com.itheima.d2_string;

import java.util.Random;
import java.util.Scanner;

/*
随机产生验证码 用String
 */
public class StringDemo3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入验证码的位数：");
        int n = sc.nextInt();
        System.out.println(createCode(n));

    }
    //定义一个方法 接收一个整型参数
    public static String createCode(int n){
        String code = "";
        String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int index = random.nextInt(62);
            char c = data.charAt(index);
            code += c;
        }
        return code;
    }
}
